package com.cwf.common.entity.http;


import com.cwf.common.enums.ResCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;


@SuppressWarnings("all")
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class R<T> {
    //响应状态码
    private int code;
    //提示信息
    private String msg;
    //响应数据
    private T data;
    private static final R response = new R();




    public R<T> writeCode(int code) {
        response.setCode(code);
        return response;
    }

    public R<T> writeMsg(String msg) {
        response.setMsg(msg);
        return response;
    }


    public R<T> writeData(T data) {
        response.setData(data);
        return response;
    }


    public static <T> R<T> success(T data) {
        response.setCode(ResCodeEnum.SUCCESS.getCode());
        response.setMsg(ResCodeEnum.SUCCESS.getMsg());
        response.setData(data);
        return response;
    }




    public static <T> R<T> error() {
        response.setCode(ResCodeEnum.ERROR.getCode());
        response.setMsg(ResCodeEnum.ERROR.getMsg());
        response.setData(null);
        return response;
    }

    public static <T> R<T> error(String msg) {
        response.setCode(ResCodeEnum.ERROR.getCode());
        response.setMsg(msg);
        response.setData(null);
        return response;
    }




    /**
     * @param msg 提示信息
     * @return
     * @description 无效参数
     * @author 18068
     * @date 2023/2/8 0:18
     */
    public static <T> R<T> paramError(String msg) {
        response.setCode(ResCodeEnum.PARAM_ERR.getCode());
        response.setMsg(msg);
        response.setData(null);
        return response;
    }


    /**
     * @description: 统一新增数据结果返回
     * @author 18068
     * @date: 2022/10/19 18:25
     */
    public static <T> R insert(T res) {
        if (check(res)) {
            return response.writeCode(ResCodeEnum.INSERT.getCode())
                    .writeMsg(ResCodeEnum.INSERT.getMsg()).writeData(null);
        } else {
            return response.writeCode(ResCodeEnum.INSERT_ERR.getCode())
                    .writeMsg(ResCodeEnum.INSERT_ERR.getMsg()).writeData(null);
        }
    }

    /**
     * @description: 统一删除数据结果返回
     * @author 18068
     * @date: 2022/10/19 18:25
     */
    public static <T> R delete(T res) {
        if (check(res)) {
            return response.writeCode(ResCodeEnum.DELETE.getCode())
                    .writeMsg(ResCodeEnum.DELETE.getMsg()).writeData(null);
        } else {
            return response.writeCode(ResCodeEnum.DELETE_ERR.getCode())
                    .writeMsg(ResCodeEnum.DELETE_ERR.getMsg()).writeData(null);
        }
    }


    /**
     * @description: 统一更新结果返回
     * @return: com.shopping.commons.utils.ResUtils<java.lang.Object>
     * @author 18068
     * @date: 2022/10/19 18:26
     */
    public static <T> R update(T res) {
        if (check(res)) {
            return response.writeCode(ResCodeEnum.UPDATE.getCode())
                    .writeMsg(ResCodeEnum.UPDATE.getMsg()).writeData(null);
        } else {
            return response.writeCode(ResCodeEnum.UPDATE_ERR.getCode())
                    .writeMsg(ResCodeEnum.UPDATE_ERR.getMsg()).writeData(null);
        }
    }

    public static <T> boolean check(Object res) {
        if (res instanceof Boolean) {
            boolean result = (boolean) res;
            return result;
        } else if (res instanceof Integer) {
            Integer result = (Integer) res;
            return result > 0;

        } else {
            log.error("数据更新响应方法参数必须是Boolean或者Integer");
            return false;
        }
    }
}
